added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBWin7TaskbarJumpList / HelperMethod.vb
blob7788069994851078cd02feaf21129cb4b599cd72
1 '********************************** Module Header **********************************\
2 ' Module Name: HelperMethods.cs
3 ' Project: CSWin7TaskbarJumpList
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' The file contains the helper methods tohandle Admin session check, restart
7 ' application to elevate the user session, register/unregister application ID and
8 ' file handle, validate file name, and create files under system temp folder.
9 '
10 ' This source is subject to the Microsoft Public License.
11 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 ' All other rights reserved.
14 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '***********************************************************************************/
19 #Region "Imports directive"
20 Imports System.Security.Principal
21 Imports System.IO
22 Imports Microsoft.Win32
23 #End Region
26 Public Class HelperMethod
28 ' Shared registry key (HKCU or HKCR)
29 Shared classRoot As RegistryKey
32 ' Helper method to check whether the current application is runas Admin
33 Public Shared Function IsAdmin() As Boolean
34 Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
35 Dim principal As WindowsPrincipal = New WindowsPrincipal(id)
36 Return principal.IsInRole(WindowsBuiltInRole.Administrator)
37 End Function
40 ' Helper method to restart the current application to evelate it to Admin session
41 Public Shared Sub RestartApplicationAsAdmin()
42 Dim proc As ProcessStartInfo = New ProcessStartInfo()
43 proc.UseShellExecute = True
44 ' Get the current application directory
45 proc.WorkingDirectory = Environment.CurrentDirectory
46 ' Get the current application executable file path
47 proc.FileName = Application.ExecutablePath
48 proc.Verb = "runas" ' Elevate the privilege
49 Process.Start(proc) ' Restart the current process
50 End Sub
53 ' Helper method to check whether the current application has registered some file handle
54 Public Shared Function IsApplicationRegistered(ByVal appid As String) _
55 As Boolean
56 Try
57 ' Open the current application's AppID key under HKCR
58 Dim progIdKey As RegistryKey = Registry.ClassesRoot.OpenSubKey(appid)
60 If Not progIdKey Is Nothing Then
61 progIdKey.Close()
62 Return True
63 End If
64 Catch ex As Exception
65 MessageBox.Show(ex.Message, ex.Source)
66 End Try
67 Return False
68 End Function
71 ' Call InternalRegisterFileAssociations method to register the file handle
72 Public Shared Sub RegisterFileAssociations( _
73 ByVal progId As String, ByVal registerInHKCU As Boolean, ByVal appId As String, _
74 ByVal openWith As String, ByVal ParamArray extensions() As String)
75 InternalRegisterFileAssociations(False, progId, registerInHKCU, appId, openWith, extensions)
76 End Sub
79 ' Call InternalRegisterFileAssociations method to unregister the file handle
80 Public Shared Sub UnregisterFileAssociations( _
81 ByVal progId As String, ByVal registerInHKCU As Boolean, ByVal appId As String, _
82 ByVal openWith As String, ByVal ParamArray extensions() As String)
83 InternalRegisterFileAssociations(True, progId, registerInHKCU, appId, openWith, extensions)
84 End Sub
87 ' Private helper method to register/unregister application file handle
88 Private Shared Sub InternalRegisterFileAssociations( _
89 ByVal unregister As Boolean, ByVal progId As String, ByVal registerInHKCU As Boolean, _
90 ByVal appId As String, ByVal openWith As String, ByVal associationsToRegister() As String)
91 Try
92 ' Check whether to register the file handle under HKCU or HKCR
93 If registerInHKCU Then
94 classRoot = Registry.CurrentUser.OpenSubKey("Software\Classes")
95 Else
96 classRoot = Registry.ClassesRoot
97 End If
99 ' First of all, unregister the file handle
100 For Each assoc In associationsToRegister
101 UnregisterFileAssociation(progId, assoc)
102 Next
103 UnregisterProgId(progId)
105 ' Register the application ID and the file handle for each file extension
106 If Not unregister Then
107 RegisterProgId(progId, appId, openWith)
108 For Each assoc In associationsToRegister
109 RegisterFileAssociation(progId, assoc)
110 Next
111 End If
112 Catch ex As Exception
113 MessageBox.Show(ex.Message)
114 End Try
115 End Sub
118 ' Register the application ID
119 Private Shared Sub RegisterProgId(ByVal progId As String, ByVal appId As String, ByVal openWith As String)
120 Dim progIdKey = classRoot.CreateSubKey(progId)
121 progIdKey.SetValue("FriendlyTypeName", "@shell32.dll,-8975")
122 progIdKey.SetValue("DefaultIcon", "@shell32.dll,-47")
123 progIdKey.SetValue("CurVer", progId)
124 progIdKey.SetValue("AppUserModelID", appId)
125 Dim shell = progIdKey.CreateSubKey("shell")
126 shell.SetValue(String.Empty, "Open")
127 shell = shell.CreateSubKey("Open")
128 shell = shell.CreateSubKey("Command")
129 shell.SetValue(String.Empty, openWith)
130 shell.Close()
131 progIdKey.Close()
132 End Sub
135 ' Unregister the application ID
136 Private Shared Sub UnregisterProgId(ByVal progId As String)
137 classRoot.DeleteSubKeyTree(progId)
138 End Sub
141 ' Register the file handle
142 Private Shared Sub RegisterFileAssociation(ByVal progId As String, ByVal extension As String)
143 Dim openWithKey = classRoot.CreateSubKey(Path.Combine(extension, "OpenWithProgIds"))
144 openWithKey.SetValue(progId, String.Empty)
145 openWithKey.Close()
146 End Sub
149 ' Unregister the file handle
150 Private Shared Sub UnregisterFileAssociation(ByVal progId As String, ByVal extension As String)
151 Dim openWithKey = classRoot.CreateSubKey(Path.Combine(extension, "OpenWithProgIds"))
152 openWithKey.DeleteValue(progId)
153 openWithKey.Close()
154 End Sub
157 ' A helper method to check whether a file name is valid
158 Public Shared Function CheckFileName(ByVal fileName As String) As Boolean
159 If Not fileName.IndexOfAny(Path.GetInvalidFileNameChars()) = -1 Then
160 MessageBox.Show("Please use only characters that are allowed in file names.")
161 Return False
162 End If
163 Return True
164 End Function
167 ' A helper method to create a file in the temp folder
168 Public Shared Function GetTempFileName(ByVal fileName As String)
169 Dim path As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), fileName + ".txt")
170 ' Ensure the file exists
171 File.Create(path).Close()
172 Return path
173 End Function
175 End Class